package com.dxq.test.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dxq.test.App;
import com.dxq.test.common.AppManager;
import com.dxq.test.common.Constant;
import com.dxq.test.R;
import com.dxq.test.adapter.StockDetailAdapter;
import com.dxq.test.db.Label;
import com.dxq.test.db.Pasture;
import com.dxq.test.db.TablesDao;
import com.j256.ormlite.dao.ForeignCollection;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: zsh
 * Date: 2023/11/17
 * Describe: 牲畜查询列表页面（创建修改群成员的时候使用）
 */
@RequiresApi(api = Build.VERSION_CODES.N)
public class StockSelectActivity extends AppCompatActivity implements View.OnClickListener {

    public TablesDao tablesDao;

    private ImageView ivBack, ivMore;
    private TextView tvTitle, tvSet;
    private RecyclerView recyclerView;

    private StockDetailAdapter mAdapter;
    private List<Label> recordList = new ArrayList<>();

    private int checkSize = 0;
    private boolean isCheckAll = false;

    private int selectSex = 0;//0:查询全部  1:查询公  2:查询母

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //添加Activity到堆栈
        AppManager.getAppManager().addActivity(this);
        setContentView(R.layout.activity_stock_select);
        selectSex = getIntent().getIntExtra("selectSex", 0);
        tablesDao = new TablesDao(this);
        initView();
        initAdapter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在appManager结束当前activity
        AppManager.getAppManager().finishActivity(this);
    }

    private void initView() {
        ivBack = findViewById(R.id.ivBack);
        ivMore = findViewById(R.id.ivMore);
        tvTitle = findViewById(R.id.tvTitle);
        tvSet = findViewById(R.id.tvSet);
        tvTitle.setText("牲畜搜索");
        tvSet.setVisibility(View.GONE);

        recyclerView = this.findViewById(R.id.recyclerView);

        ivBack.setOnClickListener(this);
        tvSet.setOnClickListener(this);
    }

    private void initAdapter() {
        recyclerView.setLayoutManager(new GridLayoutManager(this, 1));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));//添加分割线
        mAdapter = new StockDetailAdapter(R.layout.item_group_detail, recordList);//
        recyclerView.setAdapter(mAdapter);
        mAdapter.setEmptyView(R.layout.layout_empty);

        //item点击事件
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter,
                                    @NonNull @NotNull View view, int position) {
                //ToastUtils.showShort("item事件");
                //数据是使用Intent返回
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("label", recordList.get(position));
                //把返回数据存入Intent
                intent.putExtras(bundle);
                //设置返回数据
                StockSelectActivity.this.setResult(RESULT_OK, intent);
                //关闭Activity
                StockSelectActivity.this.finish();
            }
        });

        //item子view点击事件
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter,
                                         @NonNull @NotNull View view, int position) {
                //ToastUtils.showShort("child添加事件");
                Label label = recordList.get(position);
                label.setCheck(!label.isCheck());
                refreshData();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack:
                StockSelectActivity.this.finish();
                break;
        }
    }

    private void getData() {
        recordList.clear();
        List<Label> labels = tablesDao.queryLabels(App.select_pasture_id, Constant.BREEDSTATUS_CUNLAN, false);
        if (null != labels) {
            for (Label label : labels) {
                if (selectSex == 1) {
                    if (label.getSex().equals(Constant.SEX_MAN))
                        recordList.add(label);
                } else if (selectSex == 2) {
                    if (label.getSex().equals(Constant.SEX_WOMAN))
                        recordList.add(label);
                } else {
                    recordList.add(label);
                }
            }
        }
        refreshData();
    }

    //刷新列表数据
    private void refreshData() {
        checkSize = 0;
        if (null != recordList && recordList.size() > 0) {
            for (Label label : recordList) {
                if (label.isCheck()) {
                    checkSize++;
                }
            }
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onBackPressed() {
        this.finish();
    }
}
